Skip to content

Add CategoricalEncoder combining OneHotEncoder and TargetEncoder - #2244

Draft
faithlesstomas wants to merge 5 commits into
skrub-data:mainfrom
faithlesstomas:categorical-encoder
Draft

Add CategoricalEncoder combining OneHotEncoder and TargetEncoder#2244
faithlesstomas wants to merge 5 commits into
skrub-data:mainfrom
faithlesstomas:categorical-encoder

Conversation

@faithlesstomas

Copy link
Copy Markdown

New Feature Pull Request

Description

This PR introduces CategoricalEncoder, a single-column transformer that combines OneHotEncoder (for encoding frequent categories into one-hot binary columns) and TargetEncoder (for encoding categories using the target variable y).

Fixes #2065

Checklist

  • I have read the contributing guidelines
  • I have added tests that verify the feature works
  • I have added necessary documentation:
    • Docstrings for new functions/classes
    • API documentation if applicable
    • Example notebook or script demonstrating usage
  • I have added an entry to CHANGES.rst describing the feature
  • My code follows the code style of this project
  • I have checked my code and corrected any misspellings

If the feature inherits from SingleColumnTransformer:

  • get_feature_names_out has been implemented

How Has This Been Tested?

  • Added unit tests in skrub/tests/test_categorical_encoder.py covering:
    • Transformation on both Pandas and Polars Series.
    • Validation when target y is missing (y=None).
    • Cloning and isolation when custom one_hot_encoder or target_encoder instances are provided.
    • Integration with ApplyToCols.
    • Scikit-learn estimator compatibility (check_is_fitted, clone).
  • Ran local test suites (pytest skrub/tests/test_categorical_encoder.py and pytest skrub/tests/test_single_column_transformer.py).
  • Verified code style and formatting with ruff check.

Example Usage

import pandas as pd
from skrub import CategoricalEncoder

s = pd.Series(["a", "b", "a", "c", "d", "e", "a", "b", "c", "d"], name="col")
y = pd.Series([1, 0, 1, 0, 1, 0, 1, 0, 1, 0])

enc = CategoricalEncoder(max_categories=3)
res = enc.fit_transform(s, y)
print(res)

@faithlesstomas
faithlesstomas marked this pull request as draft August 5, 2026 16:04
@faithlesstomas faithlesstomas changed the title Add CategoricalEncoder combining OneHotEncoder and TargetEncoder (#2065) Add CategoricalEncoder combining OneHotEncoder and TargetEncoder Aug 5, 2026
@jeromedockes

Copy link
Copy Markdown
Member

thanks a lot @faithlesstomas !!

for context, @faithlesstomas started working on this as part of the EuroScipy 2026 sprint.

(just so you know a few maintainers including myself are currently on vacation so review may be a bit slower than usual)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Encoder that combines One-hot and target encoding

2 participants